package com.sx.gongzhong.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.ImageAdapter;
import com.sx.gongzhong.adapter.ReplyListAdapter;
import com.sx.gongzhong.adapter.viewholder.DefendRoadHeadView;
import com.sx.gongzhong.model.CommentBean;
import com.sx.gongzhong.model.DefendRoadBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.net.ServiceFactory;
import com.sx.gongzhong.pest.DefendRoadDetailPest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IDefendRoadDetailView;
import com.sx.gongzhong.view.IVoidView;
import com.sx.gongzhong.widget.CustomDialog;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 全民护路详情
 */
public class DefendRoadDetailActivity extends BaseActivity implements View.OnClickListener
        , IDefendRoadDetailView, IVoidView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private ReplyListAdapter replyAdapter;
    private List<CommentBean> replyBeanList = new ArrayList<>();
    private DefendRoadBean defendRoadDetail;

    private int currentPager = 1;
    private DefendRoadHeadView defendRoadHeadView;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.recy_edit);
        setTitleName(R.string.danger_detail);
        ButterKnife.bind(this);
        setRightImage(R.mipmap.icon_nav_more);
        String protectionId = getIntent().getStringExtra("protectionId");
        if (!BaseUtil.isEmpty(protectionId)) {
            LoadDialog.show(DefendRoadDetailActivity.this);
            new DefendRoadDetailPest(this).loadDefendRoadDetail(protectionId);
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(DefendRoadDetailActivity.this);
        ToastUtils.showToast(this, message);
        if (message.equals("获取详情失败")) {
            finish();
        }
    }

    @Override
    public void loadDefendRoadDetail(DefendRoadBean detailBean) {
        LoadDialog.dismiss(DefendRoadDetailActivity.this);
        defendRoadDetail = detailBean;
        initView();
    }

    private void initView() {
        replyBeanList = new ArrayList<>();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        replyAdapter = new ReplyListAdapter(this, R.layout.item_reply_list, replyBeanList);
        replyAdapter.setEmptyView(View.inflate(this, R.layout.no_comment_view, null));
        ((TextView) replyAdapter.getEmptyView().findViewById(R.id.tv_hint)).setText("暂无答复");
        View view = LayoutInflater.from(this).inflate(R.layout.head_defend_road_view, null);
        defendRoadHeadView = new DefendRoadHeadView();
        ButterKnife.bind(defendRoadHeadView, view);
        replyAdapter.addHeaderView(view);
        replyAdapter.setHeaderAndEmpty(true);
        replyAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPager++;
                loadReplyList();
            }
        }, recyclerView);

        recyclerView.setAdapter(replyAdapter);
        if (defendRoadDetail != null) {
            showView();
            onRefresh();
        }
    }

    /**
     * 第一次加载
     */
    public void onRefresh() {
        currentPager = 1;
        loadReplyList();
    }

    private void loadReplyList() {
        new DefendRoadDetailPest(this).loadDefendRoadReplyList(defendRoadDetail.getId(), currentPager, 15);
    }

    /**
     * 显示数据
     */
    private void showView() {
        if (defendRoadDetail.getReporter() != null){
            if (!BaseUtil.isEmpty(defendRoadDetail.getReporter().getHeaderUrl())) {
                Glide.with(this).load(ServiceFactory.getUrl(defendRoadDetail.getReporter().getHeaderUrl()))
                        .apply(new RequestOptions().centerCrop().circleCrop().
                                placeholder(R.mipmap.img_head_96).error(R.mipmap.img_head_96))
                        .into(defendRoadHeadView.im_Head);
            }
            defendRoadHeadView.tv_Name.setText(defendRoadDetail.getReporter().getName());
        }
        defendRoadHeadView.tv_Time.setText(defendRoadDetail.getReportTime());
        if ("to_be_solved".equals(defendRoadDetail.getHandleStatus())) {//待处理
            defendRoadHeadView.tv_State.setText("待处理");
            defendRoadHeadView.tv_State.setBackgroundResource(R.drawable.shape_cff5b5b_12bgd);
        } else {//已解决
            defendRoadHeadView.tv_State.setText("已解决");
            defendRoadHeadView.tv_State.setBackgroundResource(R.drawable.shape_green_corner_50);
        }
        defendRoadHeadView.tv_Content.setText(defendRoadDetail.getContent());
        defendRoadHeadView.tv_Location.setText(defendRoadDetail.getAddress());
        defendRoadHeadView.tv_Look.setText("" + defendRoadDetail.getReadTimes() + "");
        if (defendRoadDetail.getFiles() != null && defendRoadDetail.getFiles().size() > 0) {
            ImageAdapter adapter = new ImageAdapter(this, R.layout.item_image, defendRoadDetail.getFiles());
            defendRoadHeadView.imgListView.setAdapter(adapter);
        } else {
            defendRoadHeadView.imgListView.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.imv_Right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imv_Right:
                if (isLogin()) {
                    String handStatus = defendRoadDetail.getHandleStatus();
                    if (defendRoadDetail.getHandleStatus().equals("to_be_solved")) {//待解决 不能删除
                        handStatus = "undelte";
                    }
                    if (defendRoadDetail.getReporter() != null){
                        createPop(defendRoadDetail.getReporter().getId(), handStatus);
                    }
                }
                break;
            case R.id.tv_ShutUp://禁言
                createDialog(2, "禁言");
                popupWindow.dismiss();
                break;
            case R.id.tv_Report://举报
                createDialog(3, "举报");
                popupWindow.dismiss();
                break;
            case R.id.tv_Delete://删除
                createDialog(4, "删除");
                popupWindow.dismiss();
                break;
            case R.id.tv_Cancel:
                popupWindow.dismiss();
                break;
        }
    }

    /**
     * 删除/举报/禁言
     */
    public void createDialog(final int type, String title) {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定" + title + "吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (type == 2) {
                    createReport("forbiddenType", defendRoadDetail.getId(), "national_road_protection");
                } else if (type == 3) {
                    createReport("reportType", defendRoadDetail.getId(), "national_road_protection");
                } else if (type == 4) {
                    LoadDialog.show(DefendRoadDetailActivity.this, "删除中...");
                    new DefendRoadDetailPest(DefendRoadDetailActivity.this).deleteDefendRoad(defendRoadDetail.getId());
                }
            }
        }).create();
        dialog.show();
    }

    @Override
    public void deleteExposure(String message) {
        ToastUtils.showToast(this, message);
        if ("删除成功".equals(message)) {
            MyRxBus.get().post(new MsgEvent<>("DefendRoad", 1, null));
            finish();
        }
    }

    @Override
    public void loadExposureReplyList(List<CommentBean> lists, int total) {
        if (currentPager == 1) {
            replyBeanList.clear();
            replyAdapter.setNewData(replyBeanList);
        }
        if (lists != null && lists.size() > 0) {
            replyBeanList.addAll(lists);
        }
        if (replyBeanList.size() >= total) {
            replyAdapter.loadMoreEnd();
        } else {
            replyAdapter.loadMoreComplete();
        }
        defendRoadHeadView.tv_Reply.setText("部门回复(" + total + ")");
        replyAdapter.notifyDataSetChanged();
    }

    @Override
    public void voidView(String string) {
        ToastUtils.showToast(this, string);
    }
}
